<?php

namespace App\Http\Library;

use Exception;
use Illuminate\Http\Client\RequestException;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;

class JuHeCarBrand
{
    /**
     * @return mixed
     * @throws RequestException
     * @throws Exception
     */
    public static function getBrands(): mixed
    {
        $key = md5("CarBrand");
        $result = Cache::get($key);
        if (!$result){
            $url = "http://apis.juhe.cn/cxdq/brand?key=01842ecbb2f8b88dca3f698c7b069996";
            $response = Http::get($url);
            $response->throw();
            if ($response->successful()){
                $res = $response->json();
                Cache::put($key,json_encode($res,JSON_UNESCAPED_UNICODE),7);
                return $res;
            }else{
                throw new Exception("暂无请求结果!");
            }
        }else{
            return json_decode($result,true);
        }
    }
}
